import request from "@/utils/request";
import {PATH_SYS_COMMON} from "@/utils/constant";
import {FileVo} from "@/api/common/file/types";

const baseUrl = PATH_SYS_COMMON + '/file/'

/**
 * 获取images/xxx下面的所有图片地址,比如avatar
 * @param path
 */
export function getImagesApi(path: string): Promise<string[]> {
    return request.get(baseUrl + 'images/' + path)
}


/**
 * 文件路径
 */
export interface FilePathDto {
    shortUrl: string;
    fullUrl: string;
}

/**
 * 文件上传
 * @param file
 * @param filePath 文件路径
 */
export function uploadFileApi(file: File, filePath = ''): Promise<FilePathDto> {
    const formData = new FormData();
    formData.append("file", file);
    formData.append("filePath", filePath);
    return request({
        url: baseUrl + 'upload',
        method: "post",
        data: formData,
        headers: {
            "Content-Type": "multipart/form-data",
        },
    });
}

/**
 * 查看某个文件夹下的图片
 * @param folder
 */
export function viewFileApi(folder: string): Promise<FileVo[]> {
    return request.get(baseUrl + 'view/' + folder)
}

/**
 * 查询图片 - 带分页
 * @param data
 */
export function getFileListApi(data: PageQuery<FileVo>): PromisePageListType<FileVo> {
    return request.post(baseUrl + 'list', data)
}

/**
 * 获取所有不重复的文件夹
 */
export function getFoldersApi(): Promise<{ folder: string }[]> {
    return request.get(baseUrl + 'folders')
}

// 无用
export async function getIconApi(url: string) {
}